package com.xu.design;

import java.util.concurrent.atomic.AtomicReference;

/**
 * 使用原子类 AtomicReference
 * @author xudenghui
 * @create 2023-08-03 9:38
 **/
public class Singleton_6 {
    private static  final AtomicReference<Singleton_6> instance = new AtomicReference<>();


    private Singleton_6(){}


    public static  final Singleton_6 getInstance(){
        for (;;){
            Singleton_6 singleton_6 = instance.get();
            if (null != singleton_6){
                return singleton_6;
            }
            instance.compareAndSet(null, new Singleton_6());
            return instance.get();

        }
    }

    public static void main(String[] args) {
        System.out.println(Singleton_6.getInstance());
        System.out.println(Singleton_6.getInstance());
    }
}
